package xin.marcher.module.seller.service.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xin.marcher.module.common.constants.seller.SkuSellerRelationConstants;
import xin.marcher.module.common.core.PageResult;
import xin.marcher.module.common.enums.seller.SellerGroupStatusEnum;
import xin.marcher.module.common.enums.seller.SellerTypeEnum;
import xin.marcher.module.common.exception.BaseBizException;
import xin.marcher.module.common.exception.ProductErrorCodeEnum;
import xin.marcher.module.seller.domain.dto.SellerGroupResultDTO;
import xin.marcher.module.seller.domain.request.SellerGroupRequest;
import xin.marcher.module.seller.domain.response.SellerGroupResponse;
import xin.marcher.module.seller.repository.SellerRepository;
import xin.marcher.module.seller.service.SellerGroupService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 *
 */
@Service
public class SellerGroupServiceImpl implements SellerGroupService {

    /**
     * 卖家组，4位序列号
     */
    private static final int GROUP_WIDTH = 4;

    @Resource
    private SellerRepository sellerRepository;

    /**
     * 新增卖家组
     *
     * @param request
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public SellerGroupResultDTO saveSellerGroupInfo(SellerGroupRequest request) {
        // 1. 参数检查
        checkSaveSellerGroupRequest(request);
        // 2. sellerGroupId生成
        Long sellerGroupId = sellerRepository.generateSellerId(SkuSellerRelationConstants.SELLER_GROUP_PREFIX_NO, GROUP_WIDTH);
        request.setSellerGroupId(sellerGroupId);
        // 2. 新增卖家组数据
        sellerRepository.saveSellerGroup(request);
        return new SellerGroupResultDTO(request.getSellerGroupId(), Boolean.TRUE);
    }

    /**
     * 新增卖家组操作入参检查
     *
     * @param request
     */
    private void checkSaveSellerGroupRequest(SellerGroupRequest request) {
        if (Objects.isNull(request)) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }
        // 卖家组名称
        if (StringUtils.isEmpty(request.getSellerGroupName())) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }
        // 卖家组类型
        if (Objects.isNull(SellerTypeEnum.getByCode(request.getSellerGroupType()))) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }
        // 卖家组状态
        if (Objects.isNull(SellerGroupStatusEnum.getByCode(request.getSellerGroupStatus()))) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }
    }

    /**
     * 更新卖家组信息
     *
     * @param request
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public SellerGroupResultDTO updateSellerGroupInfo(SellerGroupRequest request) {
        // 1. 参数检查
        checkUpdateSellerGroupRequest(request);
        // 2. 修改卖家组数据
        sellerRepository.updateSellerGroup(request);
        return new SellerGroupResultDTO(request.getSellerGroupId(), Boolean.TRUE);
    }

    /**
     * 修改卖家组数据
     *
     * @param request
     */
    private void checkUpdateSellerGroupRequest(SellerGroupRequest request) {
        if (Objects.isNull(request)) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }
        if (Objects.isNull(request.getSellerGroupId())) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }
    }

    /**
     * 查询卖家组
     *
     * @param request
     * @return
     */
    @Override
    public PageResult<SellerGroupResponse> querySellerGroupInfo(SellerGroupRequest request) {
        // 1. 校验参数完整性
        checkQuerySellerGroupRequest(request);
        // 2. 根据条件分页查询出卖家组
        PageResult<SellerGroupResponse> sellerGroupResponsePage = sellerRepository.querySellerGroup(request);

        // 3.判断是否需要获取卖家组对应的卖家信息
        if (!request.getNeedSellerInfo()) {
            return sellerGroupResponsePage;
        }
        // 4.返回带有卖家数据的响应结果
        return sellerRepository.querySellerInfoByGroupList(sellerGroupResponsePage);
    }

    /**
     * 查询卖家组入参检查
     *
     * @param request
     */
    private void checkQuerySellerGroupRequest(SellerGroupRequest request) {
        if (Objects.isNull(request)) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }
        // 分页大小超过最大限制
        if (request.getPageSize() > SkuSellerRelationConstants.QUERY_MAX_PAGE_SIZE) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }
        // 卖家组ID数量超过最大限制
        if (CollectionUtils.isNotEmpty(request.getSellerGroupIdList()) &&
                CollectionUtils.size(request.getSellerGroupIdList()) > SkuSellerRelationConstants.QUERY_SELLER_INFO_MAX_ID_NUMBER) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }
    }

    /**
     * 删除卖家组
     *
     * @param sellerGroupIdList
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public SellerGroupResultDTO deleteSellerGroupInfo(List<Long> sellerGroupIdList) {
        sellerRepository.deleteSellerGroup(sellerGroupIdList);
        return new SellerGroupResultDTO(null, Boolean.TRUE);
    }
}
